﻿using System;
using System.Collections.Generic;
using PBLOG.Interfaces;
using PBLOG.Helpers;

namespace PBLOG.Collections
{
    public sealed class UsersCollection : ICollectionBase<User>
    {        
        public void Add(User item)
        {
            Utilities.CheckUser(item);

            XmlHelper.Users.WriteUser(item);
        }

        public void Add(string username)
        {
            User user = new User()
            {
                Username = username
            };

            Utilities.CheckUser(user);

            XmlHelper.Users.WriteUser(user);
        }

        public void Add(string username, string rule)
        {
            User user = new User()
            {
                Username = username,
                Rule = rule
            };

            Utilities.CheckUser(user);

            XmlHelper.Users.WriteUser(user);
        }

        public void Clear()
        {
            XmlHelper.Users.DeleteAllUsers();
        }

        public bool Exists(Predicate<User> match)
        {
            return XmlHelper.Users.List.Exists(match);
        }

        public bool Contains(User item)
        {            
            Predicate<User> matchUser;

            if (!String.IsNullOrWhiteSpace(item.Rule))
                matchUser = (User u) => u.Username == item.Username && u.Rule == item.Rule;

            else
                matchUser = (User u) => u.Username == item.Username;

            return XmlHelper.Users.List.Exists(matchUser);
        }

        public bool Contains(string username)
        {
            Predicate<User> matchUser = (User u) => u.Username == username;

            return XmlHelper.Users.List.Exists(matchUser);
        }

        public bool Contains(string username, string rule)
        {
            Predicate<User> matchUser = (User u) => u.Username == username && u.Rule == rule;

            return XmlHelper.Users.List.Exists(matchUser);
        }

        public void CopyTo(User[] array, int arrayIndex)
        {
            XmlHelper.Users.List.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return XmlHelper.Logs.List.Count; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public bool Remove(User item)
        {
            Utilities.ValidateUser(item);

            return XmlHelper.Users.DeleteUser(item);            
        }

        public bool Remove(string username)
        {
            User user = new User()
            {
                Username = username
            };

            Utilities.ValidateUser(user);

            return XmlHelper.Users.DeleteUser(user);            
        }

        public IEnumerator<User> GetEnumerator()
        {
            return XmlHelper.Users.List.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return XmlHelper.Users.List.GetEnumerator();
        }               
    }
}
